package com.tuiyun.jxgl.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.util.PWDUtil;

public class EditDriverAction {
	
	private Driver driver;
	private Driver d;
	
	private String msg;
	
	private int driverid;
	@Transactional
	public String reset(){
		Driver dri = dh.findById(driverid);
		dri.setPwd(PWDUtil.encryptPWD("123456"));
        dh.attachDirty(dri);
        return "success";
	}
	@Transactional
	public String delete(){
		Driver dri = dh.findById(driverid);		
        dh.delete(dri);
        return "success";
	}
	
	public int getDriverid() {
		return driverid;
	}
	public void setDriverid(int driverid) {
		this.driverid = driverid;
	}
	@Transactional
	public String execute(){
		if(id != -1){
			//admin edits student / driver
			Driver d1 = dh.findById(id);
			d1.setAge(age);
            d1.setRealName(realName);
            d1.setGender(gender);
            d1.setIdcard(idCard);
            d1.setVcode(vcode);
            d1.setLicenseCode(licenseCode);
            d1.setCarCode(carCode);
            d1.setLiveCity(liveCity);
            d1.setDriverRegion(region);
            d1.setNote(note);
            d1.setPhone(phone);
            dh.attachDirty(d1);
            driver = d1;
            setD(d1);
		}else{
			//driver edit itself
			Subject subject = SecurityUtils.getSubject();		
	        if (subject != null) {
	            Driver d = (Driver) subject.getPrincipal();
	            d.setAge(age);
	            d.setRealName(realName);
	            d.setGender(gender);
	            d.setIdcard(idCard);
	            d.setVcode(vcode);
	            d.setLicenseCode(licenseCode);
	            d.setCarCode(carCode);
	            d.setLiveCity(liveCity);
	            d.setNote(note);
	            d.setDriverRegion(region);
	            d.setPhone(phone);
	            dh.attachDirty(d);
	            driver = d;
	        }
		}
		
		msg = "编辑成功！";
		return "success";
	}
	
	private DriverHome dh;
	private int id = -1;
	private int age;
	private byte gender;
	private String note;
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	public String getVcode() {
		return vcode;
	}
	public void setVcode(String vcode) {
		this.vcode = vcode;
	}
	public String getLicenseCode() {
		return licenseCode;
	}
	public void setLicenseCode(String licenseCode) {
		this.licenseCode = licenseCode;
	}
	public String getCarCode() {
		return carCode;
	}
	public void setCarCode(String carCode) {
		this.carCode = carCode;
	}
	public String getLiveCity() {
		return liveCity;
	}
	public void setLiveCity(String liveCity) {
		this.liveCity = liveCity;
	}

	private String phone;
	private String address;
	private String realName;
	private String idCard;
	private String vcode;
	private String licenseCode;
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}

	private String carCode;
	private String liveCity;
	private byte region;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public byte getGender() {
		return gender;
	}
	public void setGender(byte gender) {
		this.gender = gender;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setDriver(Driver driver) {
		this.driver = driver;
	}
	public DriverHome getDh() {
		return dh;
	}
	public void setDh(DriverHome dh) {
		this.dh = dh;
	}
	public Driver getD() {
		return d;
	}
	public void setD(Driver d) {
		this.d = d;
	}
	public byte getRegion() {
		return region;
	}
	public void setRegion(byte region) {
		this.region = region;
	}
}
